<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="案例编号" v-bind="validateInfos.caseNo">
            <a-input v-model:value="formData.caseNo" placeholder="请输入案例编号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="功能名称" v-bind="validateInfos.functionName">
            <a-input v-model:value="formData.functionName" placeholder="请输入功能名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="测试方法" v-bind="validateInfos.testMethod">
	          <j-dict-select-tag v-model:value="formData.testMethod" dictCode="test_methods" placeholder="请选择测试方法" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="测试目的" v-bind="validateInfos.caseName">
            <a-input v-model:value="formData.caseName" placeholder="请输入测试目的"></a-input>
	          <!-- <a-textarea v-model:value="formData.caseName" :rows="4" placeholder="请输入测试目的" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="前提条件" v-bind="validateInfos.prerequisite">
	          <a-textarea v-model:value="formData.prerequisite" :rows="4" placeholder="请输入前提条件" :disabled="disabled" style="height:55px"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="数据需求" v-bind="validateInfos.dataRequire">
	          <a-textarea v-model:value="formData.dataRequire" :rows="4" placeholder="请输入数据需求" :disabled="disabled" style="height:55px"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="操作步骤" v-bind="validateInfos.description">
	          <a-textarea v-model:value="formData.description" :rows="4" placeholder="请输入操作步骤" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="预期结果" v-bind="validateInfos.expectResult">
	          <a-textarea v-model:value="formData.expectResult" :rows="4" placeholder="请输入预期结果" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="实际结果描述" v-bind="validateInfos.actualResult">
	          <a-textarea v-model:value="formData.actualResult" :rows="4" placeholder="请输入实际结果描述" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="截图上传" v-bind="validateInfos.executeRecordPic">
	          <j-image-upload  v-model:value="formData.executeRecordPic" :disabled="disabled" :fileMax="5"></j-image-upload>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CaseExecute.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    caseNo: '',   
    functionName: '',   
    caseExecuteStatus:'',
    testMethod: '',   
    caseName: '',   
    prerequisite: '',   
    dataRequire: '',   
    description: '',   
    expectResult: '',   
    actualResult: '',   
    executeRecordPic: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({style: { width: '100px' }})
  const wrapperCol = ref<any>({});
  // const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  // const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    caseName: [{ required: true, message: '请输入测试目的!'},],
    description: [{ required: true, message: '请输入操作步骤!'},],
    expectResult: [{ required: true, message: '请输入预期结果!'},],
    actualResult: [{ required: true, message: '请输入实际结果描述!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  function handleExecuteStatus(caseExecuteStatus) {
    formData.caseExecuteStatus=caseExecuteStatus
    console.log('handleExecuteStatus:' +JSON.stringify(formData))
  }
  /**
   * 提交数据
   */
  function submitForm() {
    // 触发表单验证
    nextTick( async () => {
      await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
    })
    
  }


  defineExpose({
    add,
    edit,
    handleExecuteStatus,
    submitForm,
    formData
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 700px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
